// Gateway
package main

import (
	"fury/gateway/libnet"
	//	"flag"

	log "github.com/cihub/seelog"
)

var (
	gw  *Gateway
	err error
)

const port string = ":10087"
const protocal string = "tcp"
const name string = "MessageService"

type Gateway struct {
	server   *libnet.Server
	protocal string
	addr     string
	name     string
}

func main() {
	StartGW()
}

func StartGW() {
	gw, err := NewGataway(protocal, name, port)
	if err != nil {
		log.Error(err.Error())
		return
	}
	gw.server.Serve(func(session *libnet.Session) {
		log.Info("client==>" + session.Conn().RemoteAddr().String() + "<== come in")
		go handleSession(gw, session)
	})
}

func NewGataway(protocalStr string, name string, addrStr string) (*Gateway, error) {
	gateway := &Gateway{
		protocal: protocalStr,
		addr:     addrStr,
		name:     name,
	}
	gateway.server, err = libnet.Listen(gateway.protocal, gateway.name, gateway.addr)
	return gateway, err
}

func handleSession(gw *Gateway, session *libnet.Session) {
	session.Process(func(msg *libnet.InBuffer) error {
		//		err := gw.parseProtocol(msg.Data, session)
		//		if err != nil {
		//			log.Error(err.Error())
		//		}

		return nil
	})
}
